layui.define(["form", "layer", "admin", "upload", "laydate"], function (exports) {
    var $ = layui.$,
        form = layui.form,
        layer = layui.layer,
        admin = layui.admin,
        laydate = layui.laydate,
        upload = layui.upload;

    //设定文件大小限制
    upload.render({
        elem: '#com-goods-list-import-btn',
        url: '/com/importComGoods',
        data: {"access_token": layui.data('layuiAdmin').access_token},
        size: 5000, //限制文件大小，单位 KB
        field: "comGoodsFile",
        accept: "file",
        exts: "xlsx|xls",
        before: function (obj) {
            layer.load(2, {time: 100 * 1000});
        },
        done: function (res) {
            layer.closeAll('loading');
            if (res.code === 0) {
                layer.alert('导入成功, 一共新增' + res.data.saveCount + "条数据, 更新了" + res.data.updateCount + "条，删除了" + res.data.deleteCount + "条", {icon: 6});
            } else {
                layer.msg("导入失败", {icon: 5, time: 1500});
            }
        }
    });

    $("#com-goods-list-export-btn").off("click").on("click", function () {
        var url = "/com/exportComGoods";
        var data = {};
        data["access_token"] = layui.data('layuiAdmin').access_token;

        var exportForm = $('<form>');
        exportForm.attr("style", "display:none");
        exportForm.attr('target', '');
        exportForm.attr('method', 'post');
        exportForm.attr('action', url);
        $.each(data, function (key, value) {
            if (value.trim().length !== 0) {
                var input = $("<input type='hidden'/>");
                input.attr("name", key);
                input.val(value);
                exportForm.append(input);
            }
        });

        $("body").append(exportForm);
        exportForm.submit();
    });

    $("#com-goods-list-export-express-btn").off("click").on("click", function () {
        var url = "/bil/exportBilExpressTo";
        var data = {};
        data["access_token"] = layui.data('layuiAdmin').access_token;

        var date = $('#com-goods-list-export-express-date').val();
        if(date.length !== 0) {
            var startDate = date.split("--")[0].trim();
            var endDate = date.split("--")[1].trim();

            data["startDate"] = startDate;
            data["endDate"] = endDate;
        }

        var exportForm = $('<form>');
        exportForm.attr("style", "display:none");
        exportForm.attr('target', '');
        exportForm.attr('method', 'post');
        exportForm.attr('action', url);
        $.each(data, function (key, value) {
            if (value.trim().length !== 0) {
                var input = $("<input type='hidden'/>");
                input.attr("name", key);
                input.val(value);
                exportForm.append(input);
            }
        });

        $("body").append(exportForm);
        exportForm.submit();
    });

    laydate.render({
        elem: '#com-goods-list-export-express-date',
        range: "--",
        done: function(value, date, endDate){
            var startDate = value.split("--")[0]
            var endDate = value.split("--")[1]
        }
    });

    exports("goodsList", {});
});